﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using ICeFramework.Common.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ICeFramework.Utilities.Text;

namespace ICeFramework.Web.Common
{
    public class WebUtil
    {
        public static NameValueCollection ParseUrlParameters(string url)
        {
            return ParseUrlParameters(url, '&');
        }

        public static NameValueCollection ParseUrlParameters(string urlOrQueryString, char separator)
        {
            Assert.ArgumentNotNull(urlOrQueryString, "urlOrQueryString");
            NameValueCollection values = new NameValueCollection();
            if (urlOrQueryString.Length != 0)
            {
                string str2;
                char ch;
                int index = urlOrQueryString.IndexOf('?');
                string text = (index >= 0) ? urlOrQueryString.Substring(index + 1) : urlOrQueryString;
                int position = 0;
                char[] stopCharacters = new char[] { '=', '&' };
                while (TextParser.ReadToken(text, stopCharacters, ref position, out str2, out ch))
                {
                    string token = string.Empty;
                    if ((ch == '=') && !TextParser.ReadToken(text, separator, ref position, out token))
                    {
                        token = text.Substring(position);
                    }
                    values.Add(str2.Trim(), HttpUtility.UrlDecode(token));
                }
            }
            return values;
        }

        public static HttpServerUtility GetServer()
        {
            HttpContext current = HttpContext.Current;
            //if (current == null)
            //{
            //    return Globals.Server;
            //}
            return current.Server;
        }

        public static bool IsSessionTimeout
        {
            get
            {
                if (HttpContext.Current.Session != null)
                {
                    if (HttpContext.Current.Session.IsNewSession)
                    {
                        string sCookieHeader = HttpContext.Current.Request.Headers["Cookie"];
                        if ((sCookieHeader != null) && (sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
        }
    }
}
